package Marks.Webshop.Service.Services;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.functors.NotNullPredicate;
import org.hamcrest.core.IsNull;

import Marks.Webshop.Database.dao.ProductDao;
import Marks.Webshop.Database.dto.ProductDto;
import Marks.Webshop.Service.Bo.ProductBo;

public class ProductService extends AbstractService<ProductBo>{

	ProductDao dao;
	
	public ProductService(ProductDao dao) {
		this.dao = dao;
	}
	
	@Override
	public Integer save(ProductBo bo) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(ProductBo bo) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(Integer pk) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public ProductBo findById(Integer pk) {
		ProductDto dto = this.dao.findById(pk);
		return this.convert(dto);
	}

	@Override
	public List<ProductBo> findAll() {
		List<ProductDto> listDto = dao.findAll();
		if(!listDto.isEmpty()){
			List<ProductBo> listBo = new ArrayList<ProductBo>();
			for(ProductDto dto : listDto){
				listBo.add(this.convert(dto));
			}
			return listBo;
		}else{
			return null;
		}
		
	}

	@Override
	public List<ProductBo> findAllLimit(int from, int to) {
		List<ProductDto> listDto = dao.findAllLimit(from, to);
		if(!listDto.isEmpty()){
			List<ProductBo> listBo = new ArrayList<ProductBo>();
			for(ProductDto dto : listDto){
				listBo.add(this.convert(dto));
			}
			return listBo;
		}else{
			return null;
		}
	}
	
	public ProductBo convert(ProductDto dto){
		if(dto == null){
			return null;
		}
		return new ProductBo(dto.getId(), dto.getName(), 
				dto.getPrice(), dto.getDescription(), dto.getPicture(), dto.isActive() );
	}
	
	public ProductDto convert(ProductBo bo){
		return new ProductDto(bo.getId(), bo.getName(), 
				bo.getPrice(), bo.getDescription(), bo.getPicture(), bo.isActive() );
	}

}
